﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using HalAfstande.Domain;
using HalAfstande.Domain.Objects;
using Timer = System.Timers.Timer;

namespace HalAfstande.GUI
{
    public partial class Form1 : Form
    {
        readonly BackgroundWorker _bw = new BackgroundWorker();
        public Form1()
        {
            InitializeComponent();
            
            dataGridView1.Visible = false;
            progressBar1.Visible = false;
            progressBar1.Step = 10;
            label8.Text = string.Empty;
        }

        private void Button1Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            button1.Enabled = false;
            dataGridView1.Visible = false;
           // progressBar1.Visible = true;
            label8.Visible = true;
            label8.Text = "Vent venligst...";
            button2.Visible = true;
            dataGridView1.Visible = true; 
            Update();
            
            var referee = new Referee(int.Parse(txtRefNumber.Text), txtName.Text, txtTelefon.Text, txtEmail.Text,
                                       txtStreet.Text, txtPostalCode.Text, txtCity.Text);
            var refAddress = string.Format("{0},{1}+{2}", referee.Address, referee.PostalCode, referee.City);

            var halInfo = new DataHandler(refAddress, ConfigurationManager.AppSettings["DataLocation"]);
            dataGridView1.DataSource = halInfo.Haller.Select(x => new { x.Name, x.Address, x.DistanceInKm, x.DistanceInTime }).ToList();
            dataGridView1.AutoSize = false;
            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.Refresh();
            
            progressBar1.Visible = false;
            label8.Visible = false;

            halInfo.BuildExportFile(referee, string.Format(ConfigurationManager.AppSettings["fileSavePath"], referee.RefereeNumber));
            button2.Enabled = true;
            button1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(string.Format(ConfigurationManager.AppSettings["fileSavePath"], txtRefNumber.Text));
        }
    }
}
